package com.hyyt.shopjsp.store.info.controller;

import java.sql.Timestamp;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;

import org.apache.log4j.Logger;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import com.hyyt.shopjsp.common.controller.BaseController;
import com.hyyt.shopjsp.store.info.pojo.ShopShopCategory;
import com.hyyt.shopjsp.store.info.pojo.ShopShopInfo;
import com.hyyt.shopjsp.store.info.service.IShopShopCategoryService;
import com.hyyt.shopjsp.store.info.service.IShopShopInfoService;
import com.hyyt.shopjsp.util.common.Page;
import com.hyyt.shopjsp.util.common.ResultMsg;
import com.hyyt.shopjsp.util.common.Utils;
import com.hyyt.shopjsp.util.json.FastJsonUtils;

/**
 * Copyright (c) 2017 ShopJsp. All Rights Reserved.
 * ============================================================================
 * 版权所有 2011 - 今 北京华宇盈通科技有限公司，并保留所有权利。
 * ----------------------------------------------------------------------------
 * 提示：在未取得SHOPJSP商业授权之前，您不能将本软件应用于商业用途，否则SHOPJSP将保留追究的权力。
 * ----------------------------------------------------------------------------
 * 官方网站：http://www.shopjsp.com
 * ============================================================================
 * @author Wang Youchun on 2017/04/10 0024下午 18:08.
 */

@Controller
@RequestMapping("/back/shopCategory")
public class ShopShopCategoryController extends BaseController{
	
	private Logger log = Logger.getLogger(ShopShopCategoryController.class);
	private String basePath = "/back/shop/shopCategory/";
	@Resource
	private IShopShopCategoryService shopShopCategoryService;//店铺分类service
	@Resource
	private IShopShopInfoService shopShopInfoService;//店铺service
	/**
	 * 进入店铺分类管理
	 * @return
	 */
	@RequestMapping("gotoShopCategoryPage")
	public String gotoShopCategoryPage(){
		return basePath+"shopCategoryList";
	}

	/**
	 * 查询所有店铺分类
	 * @param page
	 * @param response
	 */
	@RequestMapping(value="findShopCategoryList",method=RequestMethod.POST)
	@ResponseBody
	public void findShopCategoryList(Page page, String shopCategoryName,HttpServletResponse response){
		Map<String, Object> param = new HashMap<>();
		if(Utils.objectIsNotEmpty(shopCategoryName)){
			param.put("shopCategoryName",shopCategoryName.trim());
		}
		param.put("sortColumns", "shopCategoryId desc");
		int totalRecordCount =shopShopCategoryService.countShopShopCategory(param);
		page.setPageInfo(page.getPageSize(), totalRecordCount, page.getCurrentPage());
		List<ShopShopCategory> list =shopShopCategoryService.queryShopShopCategory(param, page.getPageRecordBeginIndex(), page.getPageSize());
		param.clear();
		param.put("total", totalRecordCount);// total键 存放总记录数，必须的
		param.put("rows", list);// rows键 存放每页记录 list
		ResultMsg.create(param, response);  //把内容发送到页面
	}

	/**
     * 保存或者修改店铺分类
     *
     * @param shopcategory
     * @return
     */
    @RequestMapping(value = "saveOrEditShopCategory", method = RequestMethod.POST)
    @ResponseBody
	public ResultMsg saveOrEditShopCategory(ShopShopCategory shopcategory) {
		if (Utils.objectIsNotEmpty(shopcategory)) {
			if (Utils.objectIsNotEmpty(shopcategory.getShopCategoryId())) {
				shopcategory.setLoadType("1");
				Timestamp date=new Timestamp(shopcategory.getCreateTime().getTime());
				shopcategory.setCreateTime(date);
				shopShopCategoryService.updateShopShopCategory(shopcategory);
			} else {
				Timestamp date=new Timestamp(System.currentTimeMillis());
				shopcategory.setCreateTime(date);
				shopShopCategoryService.saveShopShopCategory(shopcategory);
			}
		}
		return ResultMsg.create(true);
	}

	/**
	* 删除传入的店铺分类id
	*
	* @param shopCategoryId
	*/
	@RequestMapping(value = "delShopCategory", method = RequestMethod.POST)
	@ResponseBody
	public ResultMsg delShopCategory(String shopCategoryId) {
		if (Utils.objectIsNotEmpty(shopCategoryId)) {
			shopShopCategoryService.deleteShopShopCategory(Integer.parseInt(shopCategoryId));
		}
		return ResultMsg.create(true);
	}

	/**
     * 根据店铺分类id获得对象
     *
     * @param shopCategoryId
     */
    @RequestMapping(value = "getShopCategoryObject", method = RequestMethod.POST)
    @ResponseBody
    public ResultMsg getShopCategoryObject(Integer shopCategoryId) {
		if (Utils.objectIsNotEmpty(shopCategoryId)) {
			ShopShopCategory shopCategory = shopShopCategoryService.loadShopShopCategory(shopCategoryId);
			return ResultMsg.create(true, null, FastJsonUtils.toJSONString(shopCategory));
		}
		return ResultMsg.create(false);
	}

	/**
	 * 进入店铺
	 * @param shopCategoryId 店铺分类id
	 * @param param
	 * @return
	 */
	@RequestMapping("gotoShopInfoPageByShopCategoryId")
	public String gotoShopInfoPageByShopCategoryId(String shopCategoryId, Map<String, Object> param){
		param.put("shopCategoryId", shopCategoryId);
		return basePath+"shopInfoList";
	}

	/**
	 * 查询分类下所有店铺
	 * @param page
	 * @param response
	 */
	@RequestMapping(value="findShopInfoByShopCategoryId",method=RequestMethod.POST)
	@ResponseBody
	public void findShopInfoByShopCategoryId(String shopCategoryId,Page page, HttpServletResponse response, String shopName){
		Map<String, Object> param = new HashMap<>();
		if(Utils.objectIsNotEmpty(shopName)){
			param.put("shopName", shopName.trim());
		}

		param.put("shopCategoryId", shopCategoryId);
		param.put("sortColumns", "shopInfoId desc");
		int totalRecordCount =shopShopInfoService.countShopInfoByShopCategoryId(param);
		page.setPageInfo(page.getPageSize(), totalRecordCount, page.getCurrentPage());
		List<ShopShopInfo> list =shopShopInfoService.queryShopInfoByShopCategoryId(param, page.getPageRecordBeginIndex(), page.getPageSize());
		param.clear();
		param.put("total", totalRecordCount);// total键 存放总记录数，必须的
		param.put("rows", list);// rows键 存放每页记录 list
		ResultMsg.create(param, response);  //把内容发送到页面
	}
}
